function gamaM = GamaM_1(mm,x);
% GAMA_M_1.M       Version I.K. - 22/12/06
%   This function is used by the objective function OBJ_FCT_GMM_UNI_1
% COMMENTS:
%   GamaM_1.m uses the global variables ERROR_SERIES and INSTRUMENTS. Notation: Hamilton, ch 14.
 
global P ERROR_SERIES INSTRUMENTS;

T = size(ERROR_SERIES,1);
N = size(ERROR_SERIES,2);
p = P;
alpha = x(1:(N));
tau = 2*alpha - 1;

% Computing the gradient T x N matrix of the maive loss L(p,alpha,e) wrt e
grad_p = [];
for i=1:1:N
    grad_p = [grad_p, (tau(i) + sign(ERROR_SERIES(:,i))).*(abs(ERROR_SERIES(:,i)).^(p-1))];
end

% Computing the vector function h1(x,Vt) 
h1 = [];
for i=(mm+1):1:T
    h1 = [h1; kron(grad_p(i,:),INSTRUMENTS(i,:))];   
end
% Computing the vector function h2(x,Vt) 
h2 = [];
for i=1:1:(T-mm)
    h2 = [h2; kron(grad_p(i,:),INSTRUMENTS(i,:))];   
end
% Computing gammaM
r1 = size(h1,1);
r2 = size(h2,1); 
if (r1~=r2)
    'Error: r1 ne r2 in the computation of gama_m!'
    stop;
end
% computing gamaM according to eq 14.1.20
gamaM = h1'*h2/T;
% gamaM = 0;
% j = 1;
% while j <= r1       % computing gamaM according to eq 14.1.20
%     % Now at j out off r1
% %     j 
%     gamaM = gamaM + h1(j,:)'*h2(j,:);
%     j = j+1;
% end
% gamaM = gamaM/T;
if (size(gamaM,1)~=size(h1,2))|(size(gamaM,2)~=size(h1,2))
    stop;
end
